#ifndef __MP_UDP_SERVER_H__
#define __MP_UDP_SERVER_H__

#include "net_base.h"
#include "eventloop.h"

#include <functional>

namespace Gazella {

//remote host, remote port, message
typedef std::function<void(std::string, int, std::string&)> udpserver_recv_over_callback_t;

class UdpServer: public NetBase {
public:
    UdpServer();
    virtual ~UdpServer();

    //init
    int init(std::string host, int port) override;

    //send message to udp server
    int send_message(const std::string& remote_host, const int remote_port, const std::string& message);

    //add recv over callback
    void add_recv_over_callback(udpserver_recv_over_callback_t callback);

protected:
    int m_sd = -1;
    ev::io m_rx_io;

private:
    int  __create_udp_server(std::string host, int port);
    void __recv_cb(ev::io &w, int revents) override;
    void __send_cb(ev::io &w, int revents) override;

private:
    size_t m_buffer_size = 64 * 1024; //udp package max size
    char *m_buffer = nullptr; // udp packet max size

    udpserver_recv_over_callback_t m_recv_over_callback = nullptr;
};
}

#endif